package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.BasePageReq;
import com.base.BaseServiceImpl;
import com.conf.web.FileDel;
import com.entity.VipLevel;
import com.exception.ServiceException;
import com.repository.VipLevelRepository;
import com.req.VipLevelReq;
import com.base.BasePageRes;
import com.res.VipLevelRes;
import com.service.VipLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

@Service
public class VipLevelServiceImpl extends BaseServiceImpl<VipLevelReq,VipLevelRes, VipLevelRepository, VipLevel> implements VipLevelService {
    @Autowired
    private FileDel fileDel;
    @Override
    public VipLevelRes saveVipLevel(VipLevelReq req) {
        return this.handle(req);
    }

    @Override
    public VipLevelRes update(VipLevelReq vipLevelReq) {
        VipLevel vipLevel = this.queryT(vipLevelReq);
        if(!StringUtils.isEmpty(vipLevelReq.getIco())){
            if(!Objects.equals(vipLevelReq.getIco(),vipLevel.getIco())){
                fileDel.del(vipLevel.getIco());
            }
        }
        else{
            vipLevelReq.setIco(vipLevel.getIco());
        }
        return super.update(vipLevelReq);
    }

    @Override
    public VipLevelRes del(VipLevelReq vipLevelReq) {
        VipLevel vipLevel = this.queryT(vipLevelReq);
        fileDel.del(vipLevel.getIco());
        return super.del(vipLevelReq);
    }

    @Override
    public BasePageRes queryPage(BasePageReq pageReq) {
        if (StringUtils.isEmpty(pageReq.getPage())) {
            throw new ServiceException("请传入page");
        }
        if (StringUtils.isEmpty(pageReq.getSize())) {
            throw new ServiceException("请传入size");
        }


        QueryWrapper<VipLevel> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(VipLevel::getCrDate);
        if (!StringUtils.isEmpty(pageReq.getDes())) {
            wrapper.lambda().like(VipLevel::getLevel, pageReq.getDes()).
                    or().like(VipLevel::getId, pageReq.getDes())
                    .or().like(VipLevel::getDes, pageReq.getDes())
            ;


        }


        IPage<VipLevel> pages = new Page<>(pageReq.getPage(), pageReq.getSize());
        IPage<VipLevel> articalIPage = this.page(pages, wrapper);
        BasePageRes pageRes = this.convertIpage(articalIPage, new VipLevelRes());

        return pageRes;
    }
}
